
const ua = navigator.userAgent.toLowerCase();
const flg = ua.match(/baidu|quark|mqqbrowser|ucbrowser|MiuiBrowser/i); // 标注浏览器ua.


const hls = new Hls();
hideHandel = 0;
var opt = {
  container: document.getElementById("dp"),
  autoplay: true,  //自动播放
  loop: true, // 洗脑循环
  screenshot: false, // 截图按钮
  hotkey: true,
  airplay: true,
  preload: "auto",
  logo: "",
  video: {
    //https://douban.donghongzuida.com/20200501/1399_3c389def/index.m3u8
    // url: "http://h265.shuoyingkeji.com/shbk3/index.m3u8",
    url: "",
    type: "customHls", // customHls hls
    customType: {
      customHls: function (video, player) {
        //video.setAttribute("x5-video-player-type", "h5");  //注释去掉广告
        $(".dplayer-full-in-icon").remove();
        if (ua.match(/MiuiBrowser/i)) {
          // console.log(dp.plugins.hls.levels[0].details.fragments);
          console.log("小米专项解码流程");
        } else if (flg) {
          // alert("头部");
          // video.setAttribute()
          video.setAttribute("autoplay", "true");
          // noMedia(video);
        } else {
          hls.loadSource(video.src);
          hls.attachMedia(video);
        }
      }
    },
  },
};
const vjs = {
  autoplay: true,
  controls: true,
  preload: "auto",
  fluid: true,
  language: "zh_CN",
  inactivityTimeout: false,
  controlBar: {
    children: [
      { name: 'playToggle' }, // 播放按钮
      { name: 'currentTimeDisplay' }, // 当前已播放时间
      // { name: "timeDivider" },
      { name: 'durationDisplay' }, // 总时间
      { name: 'progressControl' }, // 播放进度条
      { // 倍数播放
        name: 'playbackRateMenuButton',
        'playbackRates': [0.5, 1, 1.5, 2]
      },
      { name: 'FullscreenToggle' }, // 全屏
      {
        name: 'volumePanel', // 音量控制
        inline: false, // 不使用水平方式
      },
    ]
  },
};
function useVjs(params) {
  $(opt.container).remove();
  let t = $("body");
  t.addClass("ybl");
  t.append("<video id='video' class='video-js vjs-default-skin vjs-big-play-centered ybl hfi'></video>");

  let vp = videojs("video", vjs, (a, b, c) => {
    vp.src({ src: url, type: "application/x-mpegURL" });
  });
  // vp.src({ src: url, type: "application/x-mpegURL" });

}
let url = sessionStorage.getItem("url");
sessionStorage.clear();
// url = "https://douban.donghongzuida.com/20200502/1454_a950891a/index.m3u8";
// url = "http://video2.nanme.top/video/265_720/index.m3u8";
// url = "/video/265_1080/index.m3u8";
if (url) {
  opt.video.url = url;
}
var destroy = true;
function noMedia() {
  // document.getElementsByClassName("dplayer-notice");
  let e = document.querySelector(".dplayer-notice");
  if (e && !e.style.display && e.innerHTML == "视频加载失败") { // 屏蔽视频加载失败报错
    e.style.display = "none";
  }

  if (opt.video.type == "customHls" && dp.switchVideo) { // 切换为普通url地址,不使用blob
    opt.video.type = "hls";
    dp.switchVideo(opt.video);
  } else {
    if (flg) {
      return;
    }
    // alert(2);
    console.log(2, dp.video.height);
    if (destroy) {
      destroy = false;
      dp.destroy();
    } else {
      useVjs();
    }
  }
}
var dp = {};
var ado = [ //广告数据.参考readme.md
  {
    img: "/favicon.ico",
    // audio: "/video/yeah.mp3",
    // video: "/video/h264.mp4",
    class: "mm",
    start: 0,
    createFun: (e) => {
      console.log("十秒");
    },
    closeFun: () => {
      console.log("显示");
    },
    url: "http://baidu.com"
  },
  {
    img: "/favicon.ico",
    class: "l0 t0",
    start: 0,
    end: -1,
    createFun: (e) => {
      // console.log(e);
    },
    url: ""
  },
  {
    img: "/favicon.ico",
    class: "r0 t0",
    start: 0,
    url: ""
  },
  {
    img: "/favicon.ico",
    class: "l0 b0",
    start: 0,
    url: ""
  },
  {
    img: "/favicon.ico",
    class: "r0 b0",
    start: 0,
    url: ""
  },
];
ad = {};
window.onload = mainFun;  //注释后，页面加载后.不需要初始化播放器.
function mainFun() {

  if (opt.video.url) {
    window.sessionStorage.clear();
    if (dp.switchVideo) {
      console.log(opt.video);
      dp.switchVideo(opt.video);
    } else {
      dp = new DPlayer(opt);
      setVideoLoading(true);
      dp.on("loadeddata", () => { // 数据加载完毕后执行方法
        // alert("加载完毕");
        $(".dplayer-controller").css("z-index", 9); // 提高层级,防止被广告覆盖
        if (flg) {
          // alert(opt.video.type);
          $(".dplayer-controller").hide();
          $(".dplayer-bezel").hide();
          $(".dplayer-controller-mask").hide();
          dp.video.controls = true;
          if (ua.match(/mqqbrowser/i)) {
            // alert("qq浏览器");
          }
          // dp.play();
          // alert("开始播放3");
          return;
        }
        // alert("加载视频完毕");
        //初始化广告  ---如果不要广告 请注释
        //ad = new mad(ado, dp.video);
        //ad.time(1);
        if (dp.video.videoHeight) {
          setVideoLoading(false);
        } else {
          if (opt.video.type == "customHls") { // 防止360不认
            noMedia();
          } else {
            if (dp.plugins.hls) { // PC
              setFragments(dp.plugins.hls.levels[0].details.fragments); // 软解准备
            } else { // 
              dp.on("durationchange", (e, a, b) => {
                if (dp.video.videoHeight) { // 360
                  // alert(3601)
                  setVideoLoading(false);
                  dp.play();
                } else {
                  // alert(361);
                  hls.on(Hls.Events.LEVEL_UPDATED, function (a, b) {
                    setFragments(b.details.fragments); // 准备软解
                  });
                  hls.loadSource(opt.video.url);
                }
              });
            }
          }
        }
      });
      dp.on("error", (e, d) => {
        // console.log("error", e, d);
        noMedia();
      });
      dp.on("timeupdate", () => {
        if (ad.time) ad.time(dp.video.currentTime);
      });
      dp.on("pause", () => {
        // alert("暂停");
        //暂停后隐藏中间广告
        //ad.show(0);
        // alert("bbb");
      });
      dp.on("play", (e, a, b) => {
        dp.video.controls = false;
        let t = $(".dplayer-hide-controller");
        if (!t.length) {
          if (hideHandel) clearTimeout(hideHandel);
          hideHandel = setTimeout(() => {
            $("#dp").addClass("dplayer-hide-controller");
          }, 2000);
        }
        console.log("播放", dp.video.videoHeight);
      });
    }
  } else {
    console.log("资源为空.开始发送消息");
    // 向父vue页面发送信息
    window.parent.postMessage(
      {
        cmd: "returnHeight",
        params: {
          success: true,
          data: document.body.scrollHeight + "px",
        },
      },
      "*"
    );
  }
};
//切换资源
function sw(url) {
  console.log("切换资源", url);
  window.sessionStorage.setItem("url", url);
  location.reload();
}

self.onmessage = function (e) {
  switch (e.data.cmd) {
    case "getFormJson":
      console.log("接受父页面发来的信息", e.data.params.url);
      sw(e.data.params.url);
      // 处理业务逻辑
      break;
    case "aa":
      console.log(dp.pl);
      break;
  }
}
function setFragments(fragments) { // 软解前准备
  let list = [], j = 0;
  fragments.map((a) => {
    list.push({
      sn: a.sn,
      url: a.url,
      start: j,
      end: (j += a.duration),
      duration: a.duration
    });
  });
  console.log("使用软解", list);
  dp.video.style.display = "none";
  if (!dp.pl) dp.pl = new Player({
    video: dp.video,
    jsLibPath: "./js/",
    list: list,
    precise: false,
    autoplay: opt.autoplay,
    dp: dp,
  });
  // dpEvent();
}
function setVideoLoading(params) {
  let icoL = document.getElementsByClassName("diplayer-loading-icon")[0];
  let icoP = document.getElementsByClassName("dplayer-mobile-play")[0];
  if (icoL && icoP) {
    if (params) {
      icoL.style.display = "block";
      icoP.style.display = "none";
    } else {
      icoL.style.display = null;
      icoP.style.display = null;
    }
  }
}

function videoEvent(params) { // 测试用.可删除
  dp.on("emptied", (e, a, b) => {
    console.log("dp", 1, "emptied", dp.video.videoHeight);
  });
  dp.on("timeupdate", (e, a, b) => {
    console.log("dp", 2, "timeupdate", dp.video.videoHeight);
  });
  dp.on("volumechange", (e, a, b) => {
    console.log("dp", 3, "volumechange", dp.video.videoHeight);
  });
  dp.on("loadstart", (e, a, b) => {
    console.log("dp", 4, "loadstart", dp.video.videoHeight);
  });
  dp.on("durationchange", (e, a, b) => {
    console.log("dp", 5, "durationchange", dp.video.videoHeight, dp.video.duration);
    // alert(dp.video.videoHeight);
  });
  dp.on("loadedmetadata", (e, a, b) => {
    console.log("dp", 6, "loadedmetadata", dp.video.videoHeight);
  });
  dp.on("canplay", (e, a, b) => {
    console.log("dp", 8, "canplay", dp.video.videoHeight);
  });
  dp.on("canplaythrough", (e, a, b) => {
    console.log("dp", 9, "canplaythrough", dp.video.videoHeight);
  });
  dp.on("progress", (e, a, b) => {
    console.log("dp", 10, "progress", dp.video.videoHeight);
  });
  dp.on("abort", (e, a, b) => {
    console.log("dp", "abort", dp.video.videoHeight);
  });
  dp.on("ended", (e, a, b) => {
    console.log("dp", "ended", dp.video.videoHeight);
  });
  dp.on("mozaudioavailable", (e, a, b) => {
    console.log("dp", "mozaudioavailable", dp.video.videoHeight);
  });
  dp.on("pause", (e, a, b) => {
    console.log("dp", "pause", dp.video.videoHeight);
  });
  dp.on("play", (e, a, b) => {
    console.log("dp", "play", dp.video.videoHeight);
  });
  dp.on("playing", (e, a, b) => {
    console.log("dp", "playing", dp.video.videoHeight);
  });
  dp.on("ratechange", (e, a, b) => {
    console.log("dp", "ratechange", dp.video.videoHeight);
  });
  dp.on("seeked", (e, a, b) => {
    console.log("dp", "seeked", dp.video.videoHeight);
  });
  dp.on("seeking", (e, a, b) => {
    console.log("dp", "seeking", dp.video.videoHeight);
  });
  dp.on("stalled", (e, a, b) => {
    console.log("dp", "stalled", dp.video.videoHeight);
  });
  dp.on("suspend", (e, a, b) => {
    console.log("dp", "suspend", dp.video.videoHeight);
  });
  dp.on("waiting", (e, a, b) => {
    console.log("dp", "waiting", dp.video.videoHeight);
  });
}
function dpEvent(params) {
  console.log(1);
  dp.on("screenshot", (e, a, b) => {
    console.log("screenshot", e, a, b);
  });
  dp.on("thumbnails_show", (e, a, b) => {
    console.log("thumbnails_show", e, a, b);
  });
  dp.on("thumbnails_hide", (e, a, b) => {
    console.log("thumbnails_hide", e, a, b);
  });
  dp.on("danmaku_show", (e, a, b) => {
    console.log("danmaku_show", e, a, b);
  });
  dp.on("danmaku_hide", (e, a, b) => {
    console.log("danmaku_hide", e, a, b);
  });
  dp.on("danmaku_clear", (e, a, b) => {
    console.log("danmaku_clear", e, a, b);
  });
  dp.on("danmaku_loaded", (e, a, b) => {
    console.log("danmaku_loaded", e, a, b);
  });
  dp.on("danmaku_send", (e, a, b) => {
    console.log("danmaku_send", e, a, b);
  });
  dp.on("danmaku_opacity", (e, a, b) => {
    console.log("danmaku_opacity", e, a, b);
  });
  dp.on("contextmenu_show", (e, a, b) => {
    console.log("contextmenu_show", e, a, b);
  });
  dp.on("contextmenu_hide", (e, a, b) => {
    console.log("contextmenu_hide", e, a, b);
  });
  dp.on("notice_show", (e, a, b) => {
    console.log("notice_show", e, a, b);
  });
  dp.on("notice_hide", (e, a, b) => {
    console.log("notice_hide", e, a, b);
  });
  dp.on("quality_start", (e, a, b) => {
    console.log("quality_start", e, a, b);
  });
  dp.on("quality_end", (e, a, b) => {
    console.log("quality_end", e, a, b);
  });
  dp.on("destroy", (e, a, b) => {
    console.log("destroy", e, a, b);
  });
  dp.on("resize", (e, a, b) => {
    console.log("resize", e, a, b);
  });
  dp.on("fullscreen", (e, a, b) => {
    console.log("fullscreen", e, a, b);
  });
  dp.on("fullscreen_cancel", (e, a, b) => {
    console.log("fullscreen_cancel", e, a, b);
  });
  dp.on("subtitle_show", (e, a, b) => {
    console.log("subtitle_show", e, a, b);
  });
  dp.on("subtitle_hide", (e, a, b) => {
    console.log("subtitle_hide", e, a, b);
  });
  dp.on("subtitle_change", (e, a, b) => {
    console.log("subtitle_change", e, a, b);
  });
}

// window.sessionStorage.setItem("url", opt.video.url);
